<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	/**
	 * @author Garry Willits
	 * @copyright 
	 * @date 10th May 2011
	 */
class Gallery  extends CI_Controller{
	
	const EDIT = 0;
	
	const SLIDE = 1;
	
	const VIEW = 2;
	
	const SINGLE = 3;
	
	const ROW = 4;
	
	const MULTI = 5;
	
	const CONFIGURE = 6;

	
	public $imgs_per_page =10;
	
	public $drilldownpath = "site/drillDown/";
	
	public $editpath = "admin/editImage/";
	
	public $mode;
	
	public $display_rows;
	
	public $maxwidth = 45;
	/*
	 * doesn't mean is bust means is doing something
	 */
	public $working = true;
	
	protected $startingCat = 0;
	
	protected $extraConfig = null;
	
	protected $dir_assets = array();
	public function __construct( $m,$s=NULL,$maxwidth=null,$category=0,$contentType='',$extraConfig = array() )
	{   
		// validate input
		if( $m != Gallery::EDIT && $m != Gallery::SLIDE && $m != Gallery::VIEW && $s != Gallery::SINGLE && $s !=Gallery::ROW && $s != Gallery::MULTI){
			throw new BadMethodCallException("Please provide the correct mode constant for the Gallery class to initialise", 500 );
		} 
		$this->maxwidth = $maxwidth;
		$this->mode = $m;
		$this->display_rows = $s;
		$this->startingCat = $category;
		$this->extraConfig = $extraConfig;
		parent::__construct();
		$this->load->database();
		$this->display = new GalleryDisplay();
		if($m == self::VIEW ){
			$this->display = new Display();
			$this->display->base = 'templates/multiple_view_gallery';
		}
		if( $contentType ){
			$extraNavContent="";
			if(!empty($extraConfig["extraNavContent"])){
				$extraNavContent = "/".$extraConfig["extraNavContent"];
			}
			$this->display->additional_params = "/contentType/$contentType$extraNavContent";
		}
		$this->display->id = "gallery";
		$this->display->class = "gallery";
		$this->load->database();
		$this->load->helper(array('form', 'url','path','file'));
		$this->display->scripts = 'jquery.js';
		$this->display->scripts = 'jquery-ui.js';
		$this->display->scripts = 'uiinit.js';
		$this->display->scripts = 'jquery.lightbox-0.5.js';
		$this->display->styles = 'ui';
		$this->display->styles = 'themes/smoothness/jquery.ui.all';
		$this->display->styles = 'lightbox';
		$this->display->styles = 'common';
		$this->initGallery();
		$this->display->finalize();
	}
	/**
	 * 
	 * Spits the images in the requested gallery into the $dir_assets array
	 */
	protected function initGallery(){
		$this->load->library('pagination');
		$displaying = $this->imgs_per_page;
		$cur = $this->uri->segment(6, 0);
		$this->load->model('GalleryModel','gmodel');
		$category_id = $this->uri->segment(4, $this->startingCat );
		$this->display->category_id = $category_id;
		$query = $this->gmodel->getCategory($category_id);
		$cat_num = $this->gmodel->countCategory();
		
		if($cat_num > 1){
			if( $category_id < $cat_num - 1 ){
				$this->display->next_cat = ($category_id + 1)."/page/0";
			}
			$this->display->previous_cat = ($category_id - 1)."/page/0";
		}
		else {
			$this->display->previous_cat = -1;
		}
		$this->display->gallery_title = $query->name;
		$categoryID = $query->id;
		$query =$this->gmodel->getImages($query->id,$displaying,$cur);
		// got category
		$imgs = array();
		$ci = & get_instance();
		$config = array(
				"maxwidth" => $this->maxwidth,
				"editpath" => $this->editpath,
				"category_id" => $categoryID,
				"img_per_page" => $this->imgs_per_page
			);
		$config = array_merge( $config,$this->extraConfig  );
		switch ($this->mode){
			case Gallery::EDIT;
 			$view = new AdminGalleryView($this->display, $query,$ci );
 			$view->render( $config ); 
			break;
			case Gallery::CONFIGURE;
 			$view = new ConfigureGalleryView($this->display, $query,$ci );
 			$view->render( $config ); 
			break;
			default:
			$ci = & get_instance();
 			$view = new AdminGalleryView($this->display, $query,$ci );
 			$view->render( $config ); 
		}
	}

}